/**
 * @Description: 状态-用户信息
 * @Author: rcc
 * @Date: 2024-8-30 12:52:50
 */

import { computed, ref } from "vue";
import { defineStore } from "pinia";

import { RoleTypes } from "@/types/enums.ts";
import type { User, UserData } from "@/types/api.d.ts";

export const useUserStore = defineStore("user", () => {
	const isLogin = ref(false);
	const user = uni.getStorageSync("userinfo") || {
		mobile: "",
		avatar: "",
		nickname: "",
		username: "",
		token: "",
	};
	const userInfo = ref(user);
	const userCompany = ref(null);

	const role = ref(uni.getStorageSync("role") || RoleTypes.Job);
	const roleNum = ref(uni.getStorageSync("roleNum") || 0);
	const isJob = computed(() => {
		return role.value === RoleTypes.Job;
	});

	const isHiring = computed(() => {
		return role.value === RoleTypes.Hiring;
	});

	/**
	 * 获取用户信息
	 */
	const getUserInfo = async () => {
		const res = await uni.$u.post<UserData>("/user/info", {
			role: roleNum.value,
		});

		if (res.code === 1) {
			const roleType = isNotCompany(res.data.company)
				? RoleTypes.Job
				: RoleTypes.Hiring;
			userInfo.value = res.data.userinfo;
			userCompany.value = res.data.company;
			role.value = roleType;
			uni.setStorageSync("userinfo", res.data.userinfo);
			uni.setStorageSync("role", roleType);
			uni.setStorageSync("roleNum", isNotCompany(res.data.company) ? 0 : 1);
		}
	};

	/**
	 * 检查是否登录
	 *
	 * @returns {Promise<boolean>} 登录状态.
	 */
	const checkToken = async (): Promise<boolean> => {
		const res = await uni.$u.post<UserData>("/user/info");

		if (res.code === 1) {
			userInfo.value = res.data.userinfo;
			userCompany.value = res.data.company;
			role.value = isNotCompany(res.data.company)
				? RoleTypes.Job
				: RoleTypes.Hiring;
		}

		isLogin.value = res.code === 1;

		return isLogin.value;
	};

	/**
	 * 设置用户信息
	 *
	 * @param {UserData} value - 用户信息.
	 */
	const isNotCompany = (value: any) => {
		return Object.prototype.toString.call(value) !== "[object Object]";
	};
	const setUserInfo = (value: UserData) => {
		console.log(value, 123213, 12321);
		const roleType = isNotCompany(value.company)
			? RoleTypes.Job
			: RoleTypes.Hiring;
		userInfo.value = value.userinfo;
		role.value = roleType;
		userCompany.value = value.company;
		uni.setStorageSync("userinfo", value.userinfo);
		uni.setStorageSync("role", roleType);
		uni.setStorageSync("roleNum", isNotCompany(value.company) ? 0 : 1);
	};
	// 清除用户信息
	const clearUserInfo = () => {
		userInfo.value = {
			mobile: "",
			avatar: "",
			nickname: "",
			username: "",
			token: "",
		};
		isJob.value = true;
		uni.removeStorageSync("userinfo");
		uni.removeStorageSync("role");
		uni.removeStorageSync("roleNum");
		console.log(userInfo.value, "清空");
	};

	return {
		role,
		userInfo,
		userCompany,
		isJob,
		isHiring,
		checkToken,
		clearUserInfo,
		getUserInfo,
		setUserInfo,
	};
});

export const useCommonStore = defineStore("common", () => {
	const workTimes = ref([]);
	const educations = ref([]);
	/**
	 * 获取工作年限
	 */
	const getWorkTimes = async () => {
		const res = await uni.$u.get("/user/workSelectionList");

		if (res.code === 1) {
			res.data.forEach((item: any) => {
				if (item.id == 0) item.name = "无工作经验";
			});
			workTimes.value = res.data.map((item: any) => ({
				label: item.name,
				value: item.id,
			}));
		}
	};

	/**
	 * 获取学历
	 */
	const getEducations = async () => {
		const res = await uni.$u.get("/user/educationSelectionList");

		if (res.code === 1) {
			educations.value = res.data.map((item: any) => item.name);
		}
	};

	//获取工作年限label
	const getWorkTimesLabel = (value: number) => {
		const info = workTimes.value.find((item: any) => item.value === value);
		console.log(value, workTimes.value);
		return info ? info.label : "";
	};

	return {
		workTimes,
		educations,
		getWorkTimesLabel,
		getWorkTimes,
		getEducations,
	};
});
